package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.entity.CarInformation;
import com.lin.entity.CarUserBill;
import com.lin.mapper.CarInformationMapper;
import com.lin.service.CarInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 林伟鸿
 * @since 2021-11-24
 */
@Service
@SuppressWarnings("all")
public class CarInformationServiceImpl extends ServiceImpl<CarInformationMapper, CarInformation> implements CarInformationService {

    @Autowired
    CarInformationMapper carInformationMapper;

    QueryWrapper<CarInformation> queryWrapper;


    @Override
    public DataVO<CarInformation> getCarById(Integer carId) {
        DataVO<CarInformation> dataVO = new DataVO<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id", carId);
        List<CarInformation> carInformation = carInformationMapper.selectList(queryWrapper);
        dataVO.setCount(1L);
        dataVO.setData(carInformation);
        return dataVO;
    }

    @Override
    public DataVO<CarInformation> getCarsByBrand(Integer page,Integer limit,String brand) {
        DataVO<CarInformation> dataVO = new DataVO<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        queryWrapper = new QueryWrapper<>();
        queryWrapper.like("brand", brand);
        IPage<CarInformation> iPage = new Page<>(page,limit);
        IPage<CarInformation> carInformationIPage = carInformationMapper.selectPage(iPage, queryWrapper);
        dataVO.setCount(carInformationIPage.getTotal());
        List<CarInformation> carInformationList = carInformationIPage.getRecords();
        dataVO.setData(carInformationList);
        return dataVO;

    }

    @Override
    public DataVO<CarInformation> getCarByPriceSection(Integer page, Integer limit, Integer firstPrice, Integer lastPrice) {
        DataVO<CarInformation> dataVO = new DataVO<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        queryWrapper = new QueryWrapper<>();
        queryWrapper.between("daily_price", firstPrice, lastPrice);
        IPage<CarInformation> iPage = new Page<>(page,limit);
        IPage<CarInformation> carInformationIPage = carInformationMapper.selectPage(iPage, queryWrapper);
        dataVO.setCount(carInformationIPage.getTotal());
        List<CarInformation> carInformationList = carInformationIPage.getRecords();
        dataVO.setData(carInformationList);
        return dataVO;
    }

    @Override
    public DataVO<CarInformation> findData(Integer page, Integer limit) {
        DataVO<CarInformation> dataVO = new DataVO<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        IPage<CarInformation> iPage = new Page<>(page,limit);
        queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("stock", 1);
        IPage<CarInformation> carInformationIPage = carInformationMapper.selectPage(iPage, queryWrapper);

        dataVO.setCount(carInformationIPage.getTotal());
        List<CarInformation> carInformationList = carInformationIPage.getRecords();
        dataVO.setData(carInformationList);
        return dataVO;
    }

    /**
     * 管理员查询所有汽车
     * @param page 页数
     * @param limit 条数
     * @return DataVO对象
     */
    @Override
    public DataVO<CarInformation> adminFindData(Integer page, Integer limit) {
        DataVO<CarInformation> dataVO = new DataVO<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        IPage<CarInformation> iPage = new Page<>(page,limit);
        IPage<CarInformation> carInformationIPage = carInformationMapper.selectPage(iPage, null);

        dataVO.setCount(carInformationIPage.getTotal());
        List<CarInformation> carInformationList = carInformationIPage.getRecords();
        dataVO.setData(carInformationList);
        return dataVO;
    }

    @Override
    public int addCar(CarInformation carInformation) {
        return carInformationMapper.insert(carInformation);
    }


}
